//
//  StringUtil.m
//  gdemm
//
//  Created by yongwu on 15/12/3.
//  Copyright © 2015年 goldensoft. All rights reserved.
//

#import "StringUtil.h"

@implementation StringUtil
+(BOOL)isEmpty:(NSString *)str{
    if (str == nil || str.length ==0) {
        return YES;
    }
    return NO;
}
+ (BOOL) isIp:(NSString*)str
{
    NSString* ipPattern = @"^([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}$";
    NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:ipPattern options:NSRegularExpressionCaseInsensitive error:nil];
    NSTextCheckingResult* match = [regex firstMatchInString:str options:0 range:NSMakeRange(0, [str length])];
    if (match)
    {
        return YES;
    }
    
    return NO;
}
+ (NSString*) getSuffix:(NSString*)fileName
{

    NSRange ran = [fileName rangeOfString:@"." options:NSBackwardsSearch];
    if (ran.length == 0)
    {
        return @"";
    }
    
    return [fileName substringFromIndex:ran.location + 1];
}

+ (NSString *)getFileName:(NSString *)fileName{
    NSRange ran = [fileName rangeOfString:@"&" options:NSBackwardsSearch];
    if (ran.length == 0) {
        return @"";
    }
    return [fileName substringFromIndex:ran.location + 1];
}

+ (NSString*) utf8UrlEncode:(NSString*)url
{
    NSData* data = [url dataUsingEncoding:NSUTF8StringEncoding];
    unsigned const char* utf8Str = (unsigned const char*)[data bytes];
    NSString* ret = @"";
    
    for (int i = 0; i < [data length]; ++i)
    {
        if (utf8Str[i] > 127)
        {
            ret = [ret stringByAppendingString:[NSString stringWithFormat:@"%%%02x", utf8Str[i]]];
        }
        else if (utf8Str[i] == ' ')
        {
            ret = [ret stringByAppendingString:@"%20"];
        }
        else
        {
            ret = [ret stringByAppendingString:[NSString stringWithFormat:@"%c", utf8Str[i]]];
        }
    }
    
    return ret;
}

+(NSString *)getTimeSubString:(NSString *)fileName{

    NSRange ran = [fileName rangeOfString:@"&" options:NSBackwardsSearch];
    if (ran.length == 0) {
        return nil;
    }
    return [fileName substringToIndex:ran.location];

}
@end
